<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>
			人员添加
		</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="css/main.css" media="all">
		<script src="js/axios.js"></script>
		<script src="js/md5.js"></script>
		<style>
			.imgs {
				display: none;
			}

			input[type="file"] {
				width: 50px;
				height: 50px;
				border-radius: 100%;
				opacity: 0;
				cursor: pointer;
				border: 1px solid red;
				position: absolute;
			}
		</style>

	</head>
	<body>

		<div class="x-body">
			<form class="layui-form layui-form-pane" action="" id="add">
				<blockquote class="layui-elem-quote">
					<input type="hidden" name="id" value="8">
					<div>
						<input class="layui-upload " type="file" accept="undefined" id="previewImg" name="images"
							onchange="upload(this,8)">
						<img src="./images/tx.jpg" class="layui-circle" style="width:50px;height:50px;float:left"
							id="pimages" name="pimages" alt="个人头像">
						<input id="avatar" name="image" required="" type="hidden" value="./images/tx.jpg">
						<dl style="margin-left:80px; color:#019688">
							<dt><span id="usernameSpan">ADMIN</span> <span></span></dt>
							<dd id="description" style="margin-left:0">这家伙很懒，什么也没有留下</dd>
						</dl>
					</div>
				</blockquote>

				<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">


					<div class="layui-tab-content">
						<div class="layui-tab-item layui-show">


							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>用户名
								</label>
								<div class="layui-input-block">
									<input type="text" id="username" name="name" autocomplete="off"
										placeholder="空制在80个汉字，160个字符以内" class="layui-input" value="ADMIN">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>电话号码
								</label>
								<div class="layui-input-block">
									<input id="telephone" type="text" name="phone" autocomplete="off" placeholder=""
										class="layui-input" value="15325644722">
								</div>
							</div>


							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>密码
								</label>
								<div class="layui-input-block">
									<input id="password" type="password" name="password" value="" autocomplete="off"
										placeholder="" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>新密码
								</label>
								<div class="layui-input-block">
									<input id="newpassword" type="password" name="newpassword" value=""
										autocomplete="off" placeholder="新密码为空则不修改" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<button class="layui-btn" type="button" lay-filter="add" lay-submit="">
									保存
								</button>
							</div>
							<!--</form>-->
							<div style="height:100px;"></div>
						</div>
					</div>
				</div>
			</form>
		</div>
		<script src="lib/layui/layui.js" charset="utf-8"></script>
		<script src="js/x-layui.js" charset="utf-8"></script>
		<script>
			let id = localStorage.getItem("id");
			let username = localStorage.getItem("username");
			let description = localStorage.getItem("description");
			let telephone = localStorage.getItem("telephone");
			let icon = localStorage.getItem("icon");
			document.getElementById("usernameSpan").innerHTML = username;
			document.getElementById("description").innerHTML = description;
			document.getElementById("telephone").value = telephone;
			document.getElementById("username").value = username;
			let url = "http://localhost:8080/wuye_war_exploded/UserController/showImg?fileName=";
			url = url + icon;
			axios.get(url).then((r) => {
				console.log(r)
				document.getElementById("pimages").src = "data:image/png;base64," + r.data.data;
			})
		</script>
		<script>
			layui.use(['element', 'layer', 'form'], function() {
				$ = layui.jquery; //jquery
				lement = layui.element(); //面包导航
				layer = layui.layer; //弹出层
				form = layui.form()
				//监听提交
				form.on('submit(add)', function(data) {
					let userId = id;
					let username = document.getElementById("username").value;
					let telephone = document.getElementById("telephone").value;
					let password = document.getElementById("password").value;
					let newpassword = document.getElementById("newpassword").value;
					console.log(fileName);
					let icon = fileName;
					let url = "http://localhost:8080/wuye_war_exploded/UserController/updateInfo"
					if (password == null || password == "") {

					} else {
						password = md5(md5(password + "han")).toUpperCase();
					}
					if (newpassword == null || newpassword == "") {

					} else {
						newpassword = md5(md5(newpassword + "han")).toUpperCase();
					}
					axios.post(url, {
						id: userId,
						username: username,
						password: password,
						telephone: telephone,
						newpassword: newpassword,
						icon: icon
					}).then((r) => {
						console.log(r)
						if (r.data.code == 200) {
							layer.msg(r.msg, {
								icon: 6,
								time: 2000
							}, function() {
								window.parent.location.reload();
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							});
						} else {
							layer.msg(r.msg, {
								icon: 5,
								time: 2000
							});
						}
					});
					return false;
				});

			})
		</script>
		<!--栏目缩略图上传-->
		<script>
			let fileName = null;

			function upload(obj, id) {
				var formData = new FormData();
				formData.append('images', $('#previewImg')[0].files[0]);
				formData.append('id', id);
				layer.msg('图片上传中', {
					icon: 16
				});
				let file = $('#previewImg')[0].files[0];
				let srcValue = window.URL.createObjectURL(file);
				document.getElementById("pimages").src = srcValue;
				let url = "http://localhost:8080/wuye_war_exploded/UserController/uploadIcon";
				let reader = new FileReader();


				// base64 = base64.replace(/^data:image\/.*;base64,/, '');

				reader.onload = function(e) {
					let base64 = e.target.result;
					// console.log(e.target.result)
					base64 = base64.substring(base64.indexOf(",") + 1);
					// console.log(base64);
					axios.post(url, {
						id: id,
						base64: base64
					}).then((r) => {
						console.log(r.data);
						fileName = r.data.data;
					})
				};
				reader.readAsDataURL(file);
			}
		</script>
	</body>
</html>